
//
//  U9UpLoadImage.m
//  U9WatchGame
//
//  Created by uuu9 on 2017/8/9.
//  Copyright © 2017年 UUU9. All rights reserved.
//

#import "U9UpLoadImage.h"
#import "BaseNetManager.h"


@implementation U9UpLoadImage

+ (void)modifyAvatarWithImage:(UIImage *)image andImageUrl:(NSString *)url andCompletionHandle:(void(^)(NSString *,NSError *))complete{
    NSData *imageData;
    if (url) {
        imageData =UIImageJPEGRepresentation([[UIImage alloc] initWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]], 0.5) ;
    } else {
        imageData = UIImageJPEGRepresentation(image, 0.5);
    }
    
    NSString *param = [NSString stringWithFormat:@"userid=%@&image=%@&sessionid=%@&imagetype=.png",[[NSUserDefaults standardUserDefaults] objectForKey:@"userid"],[self convertDataToHexStr:imageData],[[NSUserDefaults standardUserDefaults] objectForKey:@"sessionid"]];
    
    [BaseNetManager EncryptPOST:[NSString stringWithFormat:@"%@%@",Api_URL,kModifyAvatar]
                     parameters:param
              completionHandler:^(id responseObj, NSError *error) {
        NSString *imageUrl ;
        if (!error) {
            if (!IsNilOrNull(responseObj)) {
                @try {
                    imageUrl = responseObj[@"output"][@"image"];
                } @catch (NSException *exception) {
                    
                } @finally {
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"avatarModify" object:@{@"imageUrl":imageUrl}];
                }
            }
        }
        complete(imageUrl,error);
    }];
}

+ (void)upLoadPostImageWithImage:(UIImage *)image andCompletionHandle:(void (^)(NSString *, NSError *))complete{
    NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
    NSString *imageStr = [self convertDataToHexStr:imageData];
    NSString *param = [NSString stringWithFormat:@"userid=%@&image=%@&sessionid=%@&imagetype=.png",[[NSUserDefaults standardUserDefaults] objectForKey:@"userid"],imageStr,[[NSUserDefaults standardUserDefaults] objectForKey:@"sessionid"]];
    
    [BaseNetManager POSTBase64:[NSString stringWithFormat:@"%@%@",Api_URL,kUploadImg]
                     parameters:param
              completionHandler:^(id responseObj, NSError *error) {
        NSString *imageUrl ;
        if (!error) {
            if (!IsNilOrNull(responseObj)) {
                @try {
                    imageUrl = responseObj[@"output"][@"imagePath"];
                } @catch (NSException *exception) {
                    
                } @finally {
                    
                }
            }
        }
        complete(imageUrl,error);
    }];
}


+ (NSString *)convertDataToHexStr:(NSData *)data {
    if (!data || [data length] == 0) {
        return @"";
    }
    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
    
    [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
        unsigned char *dataBytes = (unsigned char*)bytes;
        for (NSInteger i = 0; i < byteRange.length; i++) {
            NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
            if ([hexStr length] == 2) {
                [string appendString:hexStr];
            } else {
                [string appendFormat:@"0%@", hexStr];
            }
        }
    }];
    
    return string;
}

@end
